package 设计模式.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @功能
 * @创建时间 2020/5/4
 * 自定义聚合类
 */
public class ConcreteMyAggregate {
    private List<Object> list = new ArrayList<>();

//    public ConcreteMyAggregate(List<Object> list) {
//        this.list = list;
//    }

    public void addObject(Object obj) {
        this.list.add(obj);
    }
    public void rmObject(Object obj) {
        this.list.remove(obj);
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public MyIterator createInerator(){
        return new ConcreteIterator();
    }

    private class ConcreteIterator implements MyIterator{

        private int cusor;

        @Override
        public void first() {
        cusor=0;
        }

        @Override
        public void next() {
            if(cusor<list.size())
        cusor++;
        }

        @Override
        public boolean hasNext() {
            if(cusor<list.size())
                return true;
            return false;
        }

        @Override
        public boolean isFirst() {
            if (cusor==0)
                return true;
            return false;
        }

        @Override
        public boolean isLast() {
            return cusor==(list.size()-1)?true:false;
        }

        @Override
        public Object getCurrentObj() {
            return list.get(cusor);
        }
    }
}
